Function checkPERL ()
  Dim iPerlVersion, sPerlExecutable, oFileSystem, sVersion
  
  On Error Resume Next
  
  Session.Property("SCRIPTLabel") = "Checking for PERL, Version: (" & Session.Property("SCRIPTVERSIONMIN") & " to " & Session.Property("SCRIPTVERSIONMAX") & ")"
  iPerlVersion = getRegistryKey("SOFTWARE\ActiveState\ActivePerl", "CurrentVersion", False)

  If Not (iPerlVersion = "") Then
  	
  	 sPerlExecutable = getRegistryKey("SOFTWARE\ActiveState\ActivePerl\" & iPerlVersion, "", False)
  	 If Not (sPerlExecutable = "") Then
  	 	 sPerlExecutable = sPerlExecutable & "bin\perl.exe"

			 Set oFileSystem = CreateObject("Scripting.FileSystemObject")
  		 If oFileSystem.FileExists(sPerlExecutable) Then

  		   sVersion = oFileSystem.GetFileVersion(sPerlExecutable)
		   if (Version1GreaterThanVersion2(sVersion,cstr(Session.Property("SCRIPTVERSIONMIN"))) = true) AND (Version1GreaterThanVersion2(sVersion,cstr(Session.Property("SCRIPTVERSIONMAX"))) = false) then
  	       Session.Property("SCRIPTIISEXTENSION") = sPerlExecutable & " " & Chr(34) & "%s" & Chr(34) & " %s"
  	       Session.Property("SCRIPTEXECUTABLE") = sPerlExecutable
		   Session.Property("SCRIPTCHECK") = "OK"
  	       checkPERL = ERROR_SUCCESS
  	       Exit Function
  	     End if
  	   End If
  	 End If
  End If

  Session.Property("SCRIPTCHECK") = "BAD"
  checkPERL = ERROR_INSTALL_FAILURE
End Function

Function getRegistryKey (sKeyPath, sValueName, bDword) 
  Dim sComputer, oReg, sValue
  
  ' A helper function to retrieve registry values
  
	Const HKEY_LOCAL_MACHINE = &H80000002

	On Error Resume Next
	Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
 
  ' Act on specified value type
  If bDword Then
	  oReg.GetDWORDValue HKEY_LOCAL_MACHINE,sKeyPath,sValueName,sValue
	Else
	  oReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,sKeyPath, sValueName, sValue
	End If
	
	getRegistryKey = sValue 
End Function

Function Version1GreaterThanVersion2(Version1, Version2)
	
	v1 = Split(Version1,".",-1,1)
	v2 = Split(Version2,".",-1,1)
			
	lastIndex = UBound(v1,1)
	if lastIndex>UBound(v2,1) then
		ReDim Preserve v2(lastIndex)
	else
		lastIndex = UBound(v2,1)
		ReDim Preserve v1(lastIndex)
	end if

	for i=0 to lastIndex
		if clng(v1(i)) > clng(v2(i)) then
			Version1GreaterThanVersion2 = true
			exit for
		elseif clng(v1(i)) < clng(v2(i)) then
			Version1GreaterThanVersion2 = false
			exit for
		end if
	next

	set oFileSystem = nothing
End Function